//
//  ScrollViewView.m
//  LoveTrail
//
//  Created by giantumbrella on 15/6/12.
//  Copyright (c) 2015年 赵娟. All rights reserved.
//

#import "ScrollViewView.h"
#import "CommonMacro.h"
#import "UIImageView+WebCache.h"
@interface ScrollViewView ()<UIScrollViewDelegate>
@property (nonatomic, weak)UIScrollView *scorllView;
@property (nonatomic, weak)UIPageControl *pageCorl;
@property (nonatomic, strong)NSMutableArray *pics;
@property (nonatomic, strong)NSMutableArray *currentPics;
@property (nonatomic, assign)int totolPage;
@property (nonatomic, assign)int currentPage;
@property (nonatomic,strong) NSArray *webAddressArr;
@end
@implementation ScrollViewView

- (NSMutableArray *)pics
{
    if (_pics == nil) {
        _pics = [NSMutableArray array];
    }
    return _pics;
}

- (NSMutableArray *)currentPics
{
    if (_currentPics == nil) {
        _currentPics = [NSMutableArray array];
    }
    return _currentPics;
}

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addSubViews];
        
    }
    return self;
}

#pragma mark 添加视图
- (void)addSubViews
{
    self.webAddressArr = @[@"http://www.mmisy.com/data/upload/shop/adv/04606336969591517.jpg",@"http://pic1.nipic.com/2008-09-08/200898163242920_2.jpg",@"http://pica.nipic.com/2007-12-18/200712189215503_2.jpg",@"http://pic1.nipic.com/2008-08-12/200881211331729_2.jpg",@"http://pica.nipic.com/2008-05-07/20085722191339_2.jpg"];
    for (int i = 0; i < self.webAddressArr.count; i ++) {
        UIImageView *imageView = [[UIImageView alloc] init];
       [imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.webAddressArr[i]]]];
        //imageView.frame = self.view.bounds;
        [self.pics addObject:imageView];
    }
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(3 * self.frame.size.width, 0);
    scrollView.delegate = self;
    scrollView.frame = self.frame;
    [self addSubview:scrollView];
    self.scorllView = scrollView;
    
    UIPageControl *pageCorl = [[UIPageControl alloc]init];
    pageCorl.frame = CGRectMake(self.frame.size.width / 2 - 15, self.frame.size.height - 30, 30, 30);
    
    self.currentPage = 0;
    pageCorl.currentPageIndicatorTintColor = [UIColor blackColor];
    pageCorl.pageIndicatorTintColor = [UIColor whiteColor];
    [self addSubview:pageCorl];
    self.pageCorl = pageCorl;
    //scrollView开始加载数据
    [self loadData];
    //自动播放接口
    [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(picAutoRoll) userInfo:nil repeats:YES];
}

- (void)picAutoRoll
{
    
    [self.scorllView setContentOffset:CGPointMake(self.scorllView.frame.size.width + self.scorllView.frame.size.width, 0)];
    
    //    [self.scorllView setContentOffset:CGPointMake(scrollView.frame.size.width + scrollView.frame.size.width, 0) animated:YES];
    
    
}

- (void)loadData
{
    self.totolPage = (int)self.pics.count;
    //代理模式让控制器来加入数据源才需要加入下面的判断
    //    if (self.totolPage == 0) {
    //        return;
    //    }
    self.pageCorl.numberOfPages = self.totolPage;
    self.pageCorl.currentPage = self.currentPage;
    
    //移除所有scrollView的图片，如果有
    NSArray *scrollsubViews = [self.scorllView subviews];
    if (scrollsubViews.count) {
        //[scrollsubViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        //等价于下一句
        for (UIImageView *imageview in self.scorllView.subviews) {
            [imageview removeFromSuperview];
        }
    }
    //重新添加(先选择pre.current,last)三张图片到scrollview上
    [self pickPicsWithCurrentPage:self.currentPage];
    
}

- (void)pickPicsWithCurrentPage:(int)currentpage
{
    //挑选3张图片的方法
    //获取目标下标
    int last = currentpage + 1;
    int pre = currentpage - 1;
    if (currentpage == 0) {
        pre = self.totolPage - 1;
    }
    //抽成方法，但到顶或头时，清“0”
    if (currentpage == self.totolPage-1) {
        last = 0;
    }
    
    //利用下标，从总数组中获取目标3张图//如果数组有值先清空
    if (self.currentPics.count) {
        [self.currentPics removeAllObjects];
    }
    [self.currentPics addObject:self.pics[pre]];
    [self.currentPics addObject:self.pics[currentpage]];
    [self.currentPics addObject:self.pics[last]];
#warning 所以scorllview的结构没变，只不过每次都换了前后的图片而已
    //将pics的目标3图片加入scrollview
    for (int i = 0; i< 3; i++) {
        UIImageView *view = [self.currentPics objectAtIndex:i];
        view.frame = CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height);
        [self.scorllView addSubview:view];
    }
    //设置显示中间，即currentPage
#warning 很重要
    [self.scorllView setContentOffset:CGPointMake(self.frame.size.width, 0)];
    
}
//获取currentpage的值 根据原page+1
//如果到0张时，需要显示pre的值 即 倒数第一张，如果到最后一张，显示last的值，及0
- (int)getCurrentPageWithValue:(int)page
{
    if (page == -1) {
        return self.totolPage - 1;
    }else if (page == self.totolPage){
        return 0;
    }
    return page;
}
#pragma scrollView 代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //到顶和头时，currentpage要进行判断
    if (scrollView.contentOffset.x >= 2 *self.frame.size.width) {
        self.currentPage = [self getCurrentPageWithValue:self.currentPage + 1];
        [self loadData];
    }
//    NSLog(@"%f",scrollView.contentOffset.x);
    if (scrollView.contentOffset.x <= 0) {//到顶和底时，才会更新数字
        self.currentPage = [self getCurrentPageWithValue:self.currentPage - 1];
        [self loadData];
    }
}

@end
